// pages/index/schoolrank/schoolrank.js
import {
    GetSchoolRankListUrl, // 院校排行榜
} from '../../../api/index'

import Toast from '../../../miniprogram_npm/@vant/weapp/toast/toast';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    listArray:[],
    itemArray: ['综合类','强基','985','211','双一流'],
    imageBase64:'http://47.92.28.18:5099/img/applet/school_icon_rank_top.png',
    dataSourceArray:[{dictValue: 0, dictLabel: "校友会"},
    {dictValue: 1, dictLabel: "武书连"},
    {dictValue: 2, dictLabel: "软科"},
    {dictValue: 3, dictLabel: "QS"},
    {dictValue: 4, dictLabel: "USNews"}],
    pageNo:1,
        totalPages:0,
        nomore:false,
        selectIndex:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
      console.log(options);
    this.setData({
        selectIndex:parseInt(options.selectindex),
     });
     this.setData({
        listArray:[],
        pageNo: 1,
        totalPages: 0,
        nomore: false,
      })
      this._GetSchoolRankListUrl("?pageNum=" + this.data.pageNo + '&pageSize=10', this.data.selectIndex,true)
  },

  cellSelectAction(event) {
      if (this.data.selectIndex == 3 || this.data.selectIndex == 4) {
          return
      }
     wx.navigateTo({
        url: '/pages/index/schooldetail/schooldetail?id=' + event.currentTarget.dataset.id,
     })
  },

  // 切换tab
  onChange(event) {
    console.log(event);
    this.setData({
        selectIndex:event.detail.index
    })
    this.setData({
        listArray:[],
        pageNo: 1,
        totalPages: 0,
        nomore: false,
      })
      this._GetSchoolRankListUrl("?pageNum=" + this.data.pageNo + '&pageSize=10', this.data.selectIndex,true)
  },

_GetSchoolRankListUrl(data,type, isLoading) {
    console.log(data)
    console.log(this.data.totalPages)
    console.log(this.data.listArray.length)
    let that = this
    if (this.data.totalPages <= this.data.listArray.length && this.data.totalPages != 0) {
        this.setData({
          nomore: true
        })
        return;
      }
      GetSchoolRankListUrl(data,type, isLoading).then(res => {
        console.log(res)
        let code = res.code;
        // Toast('成功')
        if (code == 200) {
            let array = res.rows;
            that.setData({
                listArray: that.data.listArray.concat(array),
                pageNo: that.data.pageNo + 1,
                totalPages: res.total,
              });
 
              if (that.data.totalPages <= that.data.listArray.length && that.data.totalPages != 0) {
                    that.setData({
                        nomore: true
                    })
              } 
        } else {

            Toast(res.msg)
        }
    })
},


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    console.log('下拉刷新')
    // 显示顶部刷新图标
    wx.showNavigationBarLoading();
    this.setData({
        listArray:[],
        pageNo: 1,
        totalPages: 0,
        nomore: false,
      })
      this._GetSchoolRankListUrl("?pageNum=" + this.data.pageNo + '&pageSize=10', this.data.selectIndex,true)
    // 隐藏导航栏加载框
    wx.hideNavigationBarLoading();
    // 停止下拉动作
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    console.log('上拉加载更多')
    this._GetSchoolRankListUrl("?pageNum=" + this.data.pageNo + '&pageSize=10', this.data.selectIndex,true)
  },

  onShareTimeline: function (){

},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})